package com.hb.common.config;

import com.hb.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Random;

/**
 * @author hb
 * @date 2023/7/9
 * @description 代理服务配置
 */

@Configuration
public class ProxyConfig {

    /**
     * 代理服务ip:port  1.23.2.123:8080;1.43.231.221:8080
     */
    private String proxyServes;

    /**
     * 代理ip
     */
    protected static String[] proxyHosts;

    /**
     * 代理端口
     */
    protected static Integer[] proxyPorts;

    /**
     * 代理服务是否开启
     */
    protected static boolean flag;

    private static Random random = new Random();

    protected static int num;

    protected static int currentIndex = 0;

    /**
     * 解析代理ip和端口
     * 在初始化后触发
     */
    @ConditionalOnProperty(name = "proxy.enable", havingValue = "true")
    @PostConstruct
    public void analyze() {
        System.out.println("======测试-======");
        if (StringUtils.isNotEmpty(proxyServes)) {
            String[] proxyServeArr = proxyServes.split(";");
            int length = proxyServeArr.length;
            num = length;
            proxyHosts = new String[length];
            proxyPorts = new Integer[length];
            for (int i = 0; i < proxyServeArr.length; i++) {
                String[] serve = proxyServeArr[i].split(":");
                proxyHosts[i] = serve[0];
                proxyPorts[i] = Integer.valueOf(serve[1]);
            }
        } else {
            num = 0;
            if (flag) {
                throw new IllegalArgumentException("Http Proxy error, properties 'proxy.serve-cluster' no value");
            }
        }
    }

    public String getProxyServes() {
        return proxyServes;
    }

    @Value("${proxy.serve-cluster:}")
    protected void setProxyServes(String proxyServes) {
        this.proxyServes = proxyServes;
    }

    @Value("${proxy.enable: false}")
    protected void setFlag(boolean flag) {
        this.flag = flag;
    }

    public static boolean isFlag() {
        return flag;
    }

    public String[] getProxyHosts() {
        return proxyHosts;
    }

    public Integer[] getProxyPorts() {
        return proxyPorts;
    }

    public static int getNum() {
        return num;
    }

    public static IpHost getRandomIpHost() {
        IpHost ipHost = new IpHost();
        int length = proxyHosts.length;
        int index = random.nextInt(length);
        ipHost.setIp(proxyHosts[random.nextInt(index)]);
        ipHost.setPort(proxyPorts[random.nextInt(index)]);

        return ipHost;
    }


    public static IpHost getIpHost() {
        IpHost ipHost = null;
        if (ipHost == null) {
            synchronized (ProxyConfig.class) {
                if(ipHost == null) {
                    ipHost = new IpHost();
                }
            }
        }
        if (currentIndex == num) {
            currentIndex = 0;
        }
        ipHost.setIp(proxyHosts[currentIndex]);
        ipHost.setPort(proxyPorts[currentIndex]);
        currentIndex++;

        return ipHost;
    }


    public static class IpHost {
        private String ip;
        private Integer port;

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }

        public Integer getPort() {
            return port;
        }

        public void setPort(Integer port) {
            this.port = port;
        }
    }
}
